home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 176-200 / disk_179 / regexp / timer.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  182 lines

  1. /*
  2.  * Simple timing program for regcomp(). 
  3.  *
  4.  * Copyright (c) 1986 by University of Toronto. Written by Henry Spencer.  Not
  5.  * derived from licensed software. 
  6.  *
  7.  * Permission is granted to anyone to use this software for any purpose on any
  8.  * computer system, and to redistribute it freely, subject to the following
  9.  * restrictions: 
  10.  *
  11.  * 1. The author is not responsible for the consequences of use of this
  12.  * software, no matter how awful, even if they arise from defects in it. 
  13.  *
  14.  * 2. The origin of this software must not be misrepresented, either by explicit
  15.  * claim or by omission. 
  16.  *
  17.  * 3. Altered versions must be plainly marked as such, and must not be
  18.  * misrepresented as being the original software. 
  19.  *
  20.  * Usage: timer ncomp nexec nsub or timer ncomp nexec nsub regexp string [
  21.  * answer [ sub ] ] 
  22.  *
  23.  * The second form is for timing repetitions of a single test case. The first
  24.  * form's test data is a compiled-in copy of the "tests" file. Ncomp, nexec,
  25.  * nsub are how many times to do each regcomp, regexec, and regsub.  The way
  26.  * to time an operation individually is to do something like "timer 1 50 1". 
  27.  */
  28. #include <stdio.h>
  29.  
  30. struct try {
  31.     char           *re, *str, *ans, *src, *dst;
  32. }               tests[] = {
  33. #include "timer.t.h"
  34.     {
  35.                     NULL, NULL, NULL, NULL, NULL
  36.     }
  37. };
  38.  
  39. #include <regexp.h>
  40.  
  41. int             errreport = 0;    /* Report errors via errseen? */
  42. char           *errseen = NULL;    /* Error message. */
  43.  
  44. char           *progname;
  45.  
  46. /* ARGSUSED */
  47. main(argc, argv)
  48.     int             argc;
  49.     char           *argv[];
  50. {
  51.     int             ncomp, nexec, nsub;
  52.     struct try      one;
  53.     char            dummy[512];
  54.  
  55.     if (argc < 4) {
  56.     ncomp = 1;
  57.     nexec = 1;
  58.     nsub = 1;
  59.     } else {
  60.     ncomp = atoi(argv[1]);
  61.     nexec = atoi(argv[2]);
  62.     nsub = atoi(argv[3]);
  63.     }
  64.  
  65.     progname = argv[0];
  66.     if (argc > 5) {
  67.     one.re = argv[4];
  68.     one.str = argv[5];
  69.     if (argc > 6)
  70.         one.ans = argv[6];
  71.     else
  72.         one.ans = "y";
  73.     if (argc > 7) {
  74.         one.src = argv[7];
  75.         one.dst = "xxx";
  76.     } else {
  77.         one.src = "x";
  78.         one.dst = "x";
  79.     }
  80.     errreport = 1;
  81.     try(one, ncomp, nexec, nsub);
  82.     } else
  83.     multiple(ncomp, nexec, nsub);
  84.     exit(0);
  85. }
  86.  
  87. void
  88. regerror(s)
  89.     char           *s;
  90. {
  91.     if (errreport)
  92.     errseen = s;
  93.     else
  94.     error(s, "");
  95. }
  96.  
  97. #ifndef ERRAVAIL
  98. error(s1, s2)
  99.     char           *s1;
  100.     char           *s2;
  101. {
  102.     fprintf(stderr, "regexp: ");
  103.     fprintf(stderr, s1, s2);
  104.     fprintf(stderr, "\n");
  105.     exit(1);
  106. }
  107. #endif
  108.  
  109. int             lineno = 0;
  110.  
  111. multiple(ncomp, nexec, nsub)
  112.     int             ncomp, nexec, nsub;
  113. {
  114.     register int    i;
  115.     extern char    *strchr();
  116.  
  117.     errreport = 1;
  118.     for (i = 0; tests[i].re != NULL; i++) {
  119.     lineno++;
  120.     try(tests[i], ncomp, nexec, nsub);
  121.     }
  122. }
  123.  
  124. try(fields, ncomp, nexec, nsub)
  125.     struct try      fields;
  126.     int             ncomp, nexec, nsub;
  127. {
  128.     regexp         *r;
  129.     char            dbuf[BUFSIZ];
  130.     register int    i;
  131.  
  132.     errseen = NULL;
  133.     r = regcomp(fields.re);
  134.     if (r == NULL) {
  135.     if (*fields.ans != 'c')
  136.         complain("regcomp failure in `%s'", fields.re);
  137.     return;
  138.     }
  139.     if (*fields.ans == 'c') {
  140.     complain("unexpected regcomp success in `%s'", fields.re);
  141.     free((char *) r);
  142.     return;
  143.     }
  144.     for (i = ncomp - 1; i > 0; i--) {
  145.     free((char *) r);
  146.     r = regcomp(fields.re);
  147.     }
  148.     if (!regexec(r, fields.str)) {
  149.     if (*fields.ans != 'n')
  150.         complain("regexec failure in `%s'", "");
  151.     free((char *) r);
  152.     return;
  153.     }
  154.     if (*fields.ans == 'n') {
  155.     complain("unexpected regexec success", "");
  156.     free((char *) r);
  157.     return;
  158.     }
  159.     for (i = nexec - 1; i > 0; i--)
  160.     (void) regexec(r, fields.str);
  161.     errseen = NULL;
  162.     for (i = nsub; i > 0; i--)
  163.     regsub(r, fields.src, dbuf);
  164.     if (errseen != NULL) {
  165.     complain("regsub complaint", "");
  166.     free((char *) r);
  167.     return;
  168.     }
  169.     if (strcmp(dbuf, fields.dst) != 0)
  170.     complain("regsub result `%s' wrong", dbuf);
  171.     free((char *) r);
  172. }
  173.  
  174. complain(s1, s2)
  175.     char           *s1;
  176.     char           *s2;
  177. {
  178.     fprintf(stderr, "try: %d: ", lineno);
  179.     fprintf(stderr, s1, s2);
  180.     fprintf(stderr, " (%s)\n", (errseen != NULL) ? errseen : "");
  181. }
  182.